const app = getApp()
var netUtil = require("../../../utils/netUtil.js"); //require引入
Component({
  properties: {
    isShow: {   //navbarData   由父页面传递的数据，变量名字自命名
      type: Boolean,
      value: false
    },
    payData:{
      type: String,
      value: false
    },
    url:{
      type: String,
      value: false
    },
    order_number:{
      type: String,
      value: false
    },
    typePay:{
      type: String,
      value: false
    }
  },
  data: {
    zfbChecked: false,
    wxChecked: true,
    payWay: '',//支付方式
    canClick:true//防止多次点击
  },
  methods: {
    // 点击微信支付
    wxChange(e) {
      if (this.data.wxChecked == true) return
      if (this.data.zfbChecked === true) {
        this.setData({
          zfbChecked: false
        })
      }
      this.setData({
        payWay: 'wx',
        wxChecked: !this.data.wxChecked
      })
    },
    // 点击支付宝
    zfbChange() {
      
      if (this.data.zfbChecked == true) return
      if (this.data.wxChecked === true) {
        this.setData({
          wxChecked: false
        })
      }
      this.setData({
        payWay: 'zfb',
        zfbChecked: !this.data.zfbChecked
      })
    },
    // 点击确定支付
    goPay() {
     
      if (this.data.canClick==false) return;
      this.setData({
        canClick:false
      })
      wx.showLoading({
        title: '加载中',
      })

      if (this.data.order_number){
        if (this.data.payWay === 'zfb') {//支付宝
          wx.hideLoading()
          this.setData({
            canClick: true
          })
          wx.navigateTo({
            url: '/pages/zfbPay/zfbPay?orderNumber=' + this.data.order_number,
          })
         
        }else{
          this.wxPay(JSON.parse(this.data.order_number))
         
        }
        return
      }
      
      let params = JSON.parse(this.data.payData)
      params.pay_type = 1
      params.token = wx.getStorageSync("userInfo").token
      netUtil.postData(this.data.url, params, (datas) => {
        if (datas.code === 0) {
         
          if (this.data.payWay === 'zfb') {//支付宝
         
            wx.navigateTo({
              url: '/pages/zfbPay/zfbPay?orderNumber=' + datas.data.order_number,
            })
          } else {
            
            this.wxPay(datas.data.order_number)
          }
          
        } else {
          this.setData({
            canClick: true
          })
          wx.showToast({
            title: datas.message,
            icon: 'none'
          })
        }
      })
      
      this.setData({
        pay: true
      })
    },
    // 支付
    wxPay(order_number){
      let params={};
      params.order_number = order_number;
      params.token = wx.getStorageSync("userInfo").token
      let url=''
      if (this.data.typePay =='allpay'){
        url ="/all-pay"
      }else{
        url ="/get-wxpay-data-order"
      }
      netUtil.postData(url, params, (datas) => {
        console.log(datas)
        this.setData({
          canClick: true
        })
        if (datas.code===0){         
          wx.requestPayment({
            timeStamp: datas.data.timeStamp,
            nonceStr: datas.data.nonceStr,
            package: datas.data.package,
            signType: datas.data.signType,
            paySign: datas.data.paySign,
            success(res) { 
              wx.navigateTo({
                url: '/pages/paySuccess/paySuccess?order_number=' + order_number,
              })
            },
            fail(res) { 
              console.log(res)
            }
          })
        } else{
          wx.showToast({
            title: datas.message,
            icon:'none'
          })
        }
      })
    },
  
    // 取消支付
    // noPay() {
    //   this.setData({
    //     isShow: true
    //   })
    // },
  }
  
  

}) 


